import positionTpl from '../../views/position.art'
import positionAddTpl from '../../views/add-pos.art'
import posListTpl from '../../views/posList.art'
import positionUpdateTpl from '../../views/update-pos.art'
import pageHelper from "../../models/pageHelper";

export default () => {
    return (req, res, next) => {
        res.render(positionTpl())
        // 将新增的模态框模板挂载DOM结点中
        $('#position-add').html(positionAddTpl())
        // 将修改的模态框模板挂载DOM节点中
        $('#position-update').html(positionUpdateTpl())
        // 模态框: 挂载更新的按钮事件
        $('#position-update-submit').on('click', _positionUpdateHandler())
        // 模态框: 挂载新增的按钮事件
        $('#position-add-submit').on('click', _positionAddHandler())
        // 挂载页面信息
        _internalPageHelper()
    }
}

// 封装一层, 让该页面复用的总逻辑
const _internalPageHelper = (pageNum) => {
    pageHelper(_pageListPromise, _positionBindEvent, pageNum)
}

// 封装所有需要执行的绑定事件, 统一丢到分页器中进行加载
const _positionBindEvent = () => {
    _showPositionInfo()
    _deletePositionHandler()
}

const _showPositionInfo = () => {
    $('.pos-update-btn').on('click', function(e) {
        let id = $(this).attr('data-primary')
        $.ajax({
            url: '/api/position/one?id=' + id,
            success: function(result) {
                if( result.status === 200 ){
                    const body = result.data.position
                    $('#update-position-id').val(body._id)
                    $('#update-pos-company').val(body.companyName)
                    $('#update-pos-name').val(body.positionName)
                    $('#update-pos-city').val(body.city)
                    $('#update-pos-salary').val(body.salary)
                }
            }
        })
    })
}

const _deletePositionHandler = () => {
    $('.pos-remove-btn').on('click', function(e) {
        let id = $(this).attr('data-primary')
        $.ajax({
            url: '/api/position/remove',
            method: 'DELETE',
            data: {id},
            success: function(result) {
                if( result.status === 200 ){
                    alert(result.data.message)
                    let currentPage = $('#pager ul li[class="active"]')[0].innerText
                    let hasButtonCount = $('.pos-remove-btn').length
                    let page = hasButtonCount === 1 ? currentPage - 1 : currentPage
                    _internalPageHelper(page < 1 ? 1 : page)
                }
            }
        })
    })
}

const _pageListPromise = () => {
    return new Promise((resolve, reject) => {
        $.ajax({
            url: '/api/position/list',
            method: 'GET',
            success: function(result) {
                let pages = result.data.posList || []
                resolve({
                    pages,
                    id: '#position-list',
                    template: posListTpl
                })
            }
        })
    })
}

const _positionUpdateHandler = () => {
    return (e) => {
        $.ajax({
            url: '/api/position/update',
            method: 'PUT',
            data: $('#position-update-form').serialize(),
            success: function(result) {
                if( result.status === 200 ){
                    $('#position-update-form')[0].reset()
                    $('#update-position-dialog-close').click();
                    _internalPageHelper($('#pager ul li[class="active"]')[0].innerText)
                }else{
                    alert(result.data.message)
                }
            }
        })
    }
}

const _positionAddHandler = () => {
    return (e) => {
        $.ajax({
            url: '/api/position/add',
            method: 'POST',
            data: $('#position-add-form').serialize(),
            success: function(result) {
                if( result.status === 200 ){
                    $('#position-add-form')[0].reset()
                    $('#position-dialog-close').click();
                    _internalPageHelper()
                }else{
                    alert(result.data.message)
                }
            }
        })
    }
}

